package com.shou.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.shou.CommonVar;
import com.shou.entity.Administrator;
import com.shou.entity.User;

public class ProtalSessionHandlerInterceptor extends HandlerInterceptorAdapter{
	@Autowired
	private MessageSource messageSource;	
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		request.setAttribute("site_title", messageSource.getMessage("site_title", null, "", null));
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String url = getRequestUrl(request);
		
		//公共方法通过
		if(url.startsWith("/public/") || url.startsWith("/file/")){
			return true;
		}
		
		//登录后台管理员
		if(url.startsWith("/admin/") || url.contains("/admin/")){
			Administrator administrator = null;
			if(request.getSession() != null){
				administrator=(Administrator)request.getSession().getAttribute(CommonVar.SESSION_ADMIN);
				if(administrator!= null) {
					return true;
				}
			}
			
			if(url.startsWith("/admin/administrator/login")){
				return true;
			}
			
			if(administrator==null){
				response.sendRedirect(request.getContextPath()+"/public/login.htm");		
				return false;
			}
		}
		
//		//客户端app
//		if( url.contains("/api/userapp") || url.contains("/api/workerapp") ){
//			//request.setCharacterEncoding("UTF-8");
//		    //System.out.println("User-Agent : " + request.getHeader("User-Agent")); //返回客户端浏览器的版本号、类型 
//		    if(!"POST".equalsIgnoreCase(request.getMethod())){
//		    	return false;
//		    }
//		}
		
		//微信平台
//		if(url.contains("/phone/")){
//			
//			if(url.contains("/phone/serveInfo/weixinLogin")){
//				return true;
//			}			
//			User user = null;
//			if(request.getSession() != null){
//				user=(User)request.getSession().getAttribute(CommonVar.SESSION_WEIXIN);
//				if(user!= null) {
//					return true;
//				}
//			}
//			if(user == null){
//				response.sendRedirect(request.getContextPath()+"/phone/serveInfo/weixinLogin.htm");		
//				return false;
//			}
//		}		
		return true;
	}
	/**
	 * like "/action!method.action"
	 */
	public String getRequestUrl(HttpServletRequest request)
	{
		String url =request.getRequestURI();
		String path = request.getContextPath();
		if (StringUtils.isNotEmpty(path))
		{
			return url.substring(path.length());
		}
		return url;
	}
}
